//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.ComponentModel;
using System.Data;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace DotNetNuke.UI.WebControls
{
	[ToolboxData("<{0}:PropertyLabelControl runat=server></{0}:PropertyLabelControl>")]
	public class PropertyLabelControl : System.Web.UI.WebControls.WebControl
	{
		protected System.Web.UI.HtmlControls.HtmlGenericControl label;
		protected System.Web.UI.WebControls.LinkButton cmdHelp;
		protected System.Web.UI.WebControls.Image imgHelp;
		protected System.Web.UI.WebControls.Label lblLabel;
		protected System.Web.UI.WebControls.Panel pnlHelp;
		protected System.Web.UI.WebControls.Label lblHelp;
		private Control _EditControl;
		private string _ResourceKey;
		private object _DataSource;
		private string _DataField;
		private Style _HelpStyle = new Style();
		private Style _LabelStyle = new Style();
		protected override System.Web.UI.HtmlTextWriterTag TagKey {
			get { return HtmlTextWriterTag.Div; }
		}
		[Browsable(true), Category("Appearance"), DefaultValue("Property"), Description("Enter Caption for the control.")]
		public string Caption {
			get {
				this.EnsureChildControls();
				return lblLabel.Text;
			}
			set {
				this.EnsureChildControls();
				lblLabel.Text= value;
			}
		}
		[Browsable(false)]
		public Control EditControl {
			get { return _EditControl; }
			set { _EditControl= value; }
		}
		[Browsable(true), Category("Appearance"), DefaultValue(""), Description("Enter Help Text for the control.")]
		public string HelpText {
			get {
				this.EnsureChildControls();
				return lblHelp.Text;
			}
			set {
				this.EnsureChildControls();
				lblHelp.Text= value;
			}
		}
		[Browsable(true), Category("Localization"), DefaultValue(""), Description("Enter the Resource key for the control.")]
		public string ResourceKey {
			get { return _ResourceKey; }
			set {
				_ResourceKey= value;
				this.EnsureChildControls();
				lblHelp.Attributes["resourcekey"] = _ResourceKey + ".Help";
				imgHelp.Attributes["resourcekey"] = _ResourceKey + ".Help";
				lblLabel.Attributes["resourcekey"] = _ResourceKey + ".Text";
			}
		}
		[Browsable(true), Category("Behavior"), DefaultValue(false), Description("Set whether the Help icon is displayed.")]
		public bool ShowHelp {
			get {
				this.EnsureChildControls();
				return cmdHelp.Visible;
			}
			set {
				this.EnsureChildControls();
				cmdHelp.Visible= value;
			}
		}
		[Browsable(true), Category("Data"), DefaultValue(""), Description("Enter the name of the field that is data bound to the Label's Text property.")]
		public string DataField {
			get { return _DataField; }
			set { _DataField= value; }
		}
		[Browsable(false)]
		public object DataSource {
			get { return _DataSource; }
			set { _DataSource= value; }
		}
		[Browsable(true), Category("Styles"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), TypeConverter(typeof(ExpandableObjectConverter)), Description("Set the Style for the Help Text.")]
		public System.Web.UI.WebControls.Style HelpStyle {
			get {
				this.EnsureChildControls();
				return pnlHelp.ControlStyle;
			}
		}
		[Browsable(true), Category("Styles"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), TypeConverter(typeof(ExpandableObjectConverter)), Description("Set the Style for the Label Text")]
		public System.Web.UI.WebControls.Style LabelStyle {
			get {
				this.EnsureChildControls();
				return lblLabel.ControlStyle;
			}
		}
		protected override void CreateChildControls()
		{
			label = new HtmlGenericControl();
			label.TagName = "label";
			if (!DesignMode) {
				cmdHelp = new LinkButton();
				cmdHelp.ID = this.ID + "_cmdHelp";
				cmdHelp.CausesValidation = false;
				cmdHelp.EnableViewState = false;
				cmdHelp.TabIndex = -1;
				imgHelp = new System.Web.UI.WebControls.Image();
				imgHelp.ID = this.ID + "_imgHelp";
				imgHelp.EnableViewState = false;
				imgHelp.ImageUrl = "~/images/help.gif";
				cmdHelp.Controls.Add(imgHelp);
				label.Controls.Add(cmdHelp);
				label.Controls.Add(new LiteralControl("&nbsp;"));
			}
			lblLabel = new Label();
			lblLabel.ID = this.ID + "_label";
			lblLabel.EnableViewState = false;
			label.Controls.Add(lblLabel);
			pnlHelp = new Panel();
			pnlHelp.ID = this.ID + "_pnlHelp";
			pnlHelp.EnableViewState = false;
			lblHelp = new Label();
			lblHelp.ID = this.ID + "_lblHelp";
			lblHelp.EnableViewState = false;
			pnlHelp.Controls.Add(lblHelp);
			this.Controls.Add(label);
			this.Controls.Add(pnlHelp);
		}
		protected override void OnDataBinding(System.EventArgs e)
		{
			if (DataSource != null) {
				this.EnsureChildControls();
				if (!String.IsNullOrEmpty(DataField)) {
					DataRowView dataRow = (DataRowView)DataSource;
					if (ResourceKey == string.Empty) {
						ResourceKey = Convert.ToString(dataRow[DataField]);
					}
					if (DesignMode) {
						label.InnerText = Convert.ToString(dataRow[DataField]);
					}
				}
			}
		}
		protected override void OnPreRender(System.EventArgs e)
		{
			this.EnsureChildControls();
			DotNetNuke.UI.Utilities.DNNClientAPI.EnableMinMax(cmdHelp, pnlHelp, true, Utilities.DNNClientAPI.MinMaxPersistanceType.None);
			if (EditControl != null) {
				label.Attributes.Add("for", EditControl.ClientID);
			}
		}
		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{
			base.Render(writer);
		}
	}
}
